﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using iLangl.BusinessModel.Managers;
using iLangl.Transport.Communication;
using iLangl.Transport;

namespace iLangl.BusinessModel.Actions.Common
{
	public class PushAction : BaseSecurityAction
	{
		SyncedOneWayData _data;
		private ActionResult _actionResult = null;
		public ActionResult ActionResult
		{
			get
			{
				return _actionResult;
			}
		}


		public PushAction(SyncedOneWayData data)
		{
			_data = data;
			RequestContext = new SyncedDataRequestContext();

			ActionResultResponseContext responsecontext = new ActionResultResponseContext();
			ResponseContext = responsecontext;
			RequestContext.SetContext(data);

		}

		public override void Send()
		{
			PluginLicense pl = PluginManager.Instance.GetLicense(_data.Subscriber);

			//We not send notification if Pushing Disabled
			if (!pl.Plugin.IsPushingEnabled)
				return;

			PluginData pd = pl.Plugin;
			IsSynced = !pd.IsAsyncTransportSupported;

			
			base.Send();

			SuccessfullPush();
		}
		

		void responsecontext_ContextUpdated(object sender, EventArgs e)
		{
			object result = ResponseContext.GetContext();

			if (result is ActionResult)
			{
				_actionResult = result as ActionResult;
			}

			weGetResult();
			SuccessfullPush();
		}

		protected void SuccessfullPush()
		{
			SynchronizationManager.Instance.MarkAsSynced(_data);
		}

		protected override string GetName()
		{
			return "PushAction";
		}

		protected override string GetVersion()
		{
			return "1";
		}
	}
}
